package com.xhui.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhui.reggie.common.BaseContext;
import com.xhui.reggie.common.R;
import com.xhui.reggie.entity.ShoppingCart;
import com.xhui.reggie.service.ShoppingCartService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author XHui
 * Date 2023/1/20 15:26
 * Version 1.0
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    /**
     * 获取购物车中的数据
     * @return 返回购物车中的数据
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {

        // 获取当前已登录用户id
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        // 查询此用户 并根据创建时间 降序展示
        wrapper.eq(ShoppingCart::getUserId, userId)
                .orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> shoppingCart = shoppingCartService.list(wrapper);
        

        return R.success(shoppingCart);
    }

    /**
     * 菜品/套餐 + 1
     *
     * @param shoppingCart 总金额 菜品口味(套餐暂无) 图片 套餐/菜品名称 套餐/菜品id
     * @return 返回 已添加到购物车中的数据
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        return shoppingCartService.add(shoppingCart);
    }

    /**
     * 菜品/套餐 - 1
     * @param shoppingCart --> dishId and setmealId
     * @return 是否成功
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        return shoppingCartService.sub(shoppingCart);
    }

    /**
     * 清空购物车
     * @return 是否成功
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        return  shoppingCartService.delete() ? R.success("清空购物车成功") : R.error("清空购物车失败");
    }

}
